﻿using System;
using System.ComponentModel;

namespace Ecobee.Net
{
	/// <summary>
	/// Specifies a complex default value for a property using a simple .Net type
	/// and a IDefaultValueConverter object to convert the simple type to a 
	/// complex type. The target for this class are complex types that cannot
	/// be initialized using the standard DefaultValueAttribute.
	/// </summary>
	public class DefaultValueConverterAttribute : DefaultValueAttribute
	{
		private Type _defaultValueConverterType = null;
		private object _defaultSimpleValue = null;

		/// <summary>
		/// Initializes a new instance of DefaultValueConverterAttribute with the given IDefaultValueConverter
		/// and value that will be converted to the complex type.
		/// </summary>
		/// <param name="defaultType">A Type that represents the Type of the simpleValue parameter.</param>
		/// <param name="defaultValueConverterType">A Type that represent a IDefaultValueConverter object that 
		/// will be used to convert the specified value to the complex target type.</param>
		/// <param name="simpleValue">A value that will be converted to the default value using the 
		/// specified converter.</param>
		public DefaultValueConverterAttribute(Type defaultType, Type defaultValueConverterType, object simpleValue)
			: base(defaultType, simpleValue.ToString())
		{
			this.DefaultValueConverterType = defaultValueConverterType;
			this.SimpleValue = simpleValue;
		}

		/// <summary>
		/// Gets the default value of the property this attribute is bound to.
		/// </summary>
		public override object Value
		{
			get
			{
				object returnValue = null;

				IDefaultValueConverter converter = (IDefaultValueConverter)Activator.CreateInstance(this.DefaultValueConverterType);
				returnValue = converter.ConvertToComplexValue(this.SimpleValue);

				return returnValue;
			}
		}

		/// <summary>
		/// Gets the DefaultValueConverterType of the property this attribute is bound to.
		/// </summary>
		public Type DefaultValueConverterType
		{
			get
			{
				return _defaultValueConverterType;
			}
			private set
			{
				_defaultValueConverterType = value;
			}
		}

		/// <summary>
		/// Gets the DefaultValueConverterType of the property this attribute is bound to.
		/// </summary>
		public object SimpleValue
		{
			get
			{
				return _defaultSimpleValue;
			}
			private set
			{
				_defaultSimpleValue = value;
			}
		}
	}
}
